﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using HynuIOTA.Entity.FormatEntity;

namespace HynuIOTA.Service
{
    public abstract class ServiceBase<T> : IService.IServiceBase<T> where T : class, new()
    {
        /// <summary>
        /// IRespository 数据层仓储
        /// </summary>
        public IRespository.IRespositoryBase<T> baseRespository { get; protected set; } = null;

        public IRespository.IDBSession DBSession => DBSessionFactory.GetDBSession();

        public ServiceBase()
        {
            SetRespository();
        }

        /// <summary>
        /// 设置数据层仓储对象
        /// </summary>
        public abstract void SetRespository();

        public void Add(T model)
        {
            baseRespository.Add(model);
        }

        public void Delete(T model)
        {
            baseRespository.Delete(model);
        }

        public void DeleteBy(Expression<Func<T, bool>> whereLambda)
        {
            baseRespository.DeleteBy(whereLambda);
        }

        public void Modify(T model)
        {
            baseRespository.Modify(model);
        }

        public void Modify(T model, params string[] modifiedPropertyNames)
        {
            baseRespository.Modify(model, modifiedPropertyNames);
        }

        public void ModifyValue(T model, object obj)
        {
            baseRespository.ModifyValue(model, obj);
        }

        public void ModifyBy(Expression<Func<T, bool>> whereLamdba, Dictionary<string, object> modifyPropertyNamesValues)
        {
            baseRespository.ModifyBy(whereLamdba, modifyPropertyNamesValues);
        }

        public IQueryable<T> Where(Expression<Func<T, bool>> whereLamdba)
        {
            return baseRespository.Where(whereLamdba);
        }

        public IQueryable<T> Where(Expression<Func<T, bool>> whereLamdba, params string[] foreignKeyPropertyNames)
        {
            return baseRespository.Where(whereLamdba, foreignKeyPropertyNames);
        }

        public T WhereFristOrDefault(Expression<Func<T, bool>> whereLamdba)
        {
            return baseRespository.WhereFirstOrDefault(whereLamdba);
        }

        public IQueryable<T> WhereOrderBy<TKey>(Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            return baseRespository.WhereOrderBy(whereLamdba, orderBy, isAsc);
        }

        public IQueryable<T> WhereOrderBy<TKey>(Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderBy, bool isAsc = true, params string[] foreignKeyPropertyNames)
        {
            return baseRespository.WhereOrderBy(whereLamdba, orderBy, isAsc, foreignKeyPropertyNames);
        }

        public IQueryable<T> WherePage<TKey>(PageData pageData, Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            return baseRespository.WherePage(pageData, whereLamdba, orderBy, isAsc);
        }

        public IQueryable<T> WherePage<TKey>(PageData pageData, Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderBy, bool isAsc = true, params string[] foreignKeyPropertyNames)
        {
            return baseRespository.WherePage(pageData, whereLamdba, orderBy, isAsc, foreignKeyPropertyNames);
        }

        public Task<IQueryable<T>> WherePageAsync<TKey>(PageData pageData, Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            return baseRespository.WherePageAsync(pageData, whereLamdba, orderBy, isAsc);
        }

        public Task<IQueryable<T>> WherePageAsync<TKey>(PageData pageData, Expression<Func<T, bool>> whereLamdba, Expression<Func<T, TKey>> orderBy, bool isAsc = true, params string[] foreignKeyPropertyNames)
        {
            return baseRespository.WherePageAsync(pageData, whereLamdba, orderBy, isAsc, foreignKeyPropertyNames);
        }

    }
}
